﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Nizm0.QOT.Library.UserControls
{
    public partial class GenerateQuery : UserControl
    {

        //private ListView.ListViewItemCollection _columns;// = new ListView.ListViewItemCollection(listView_GeneratetQuery);
        private Library.Objects.ProcedureInfo _procedure;
        private Windows.frmQueryGeneratorProperties _generateProperties;
        private Library.Objects.ListOfVariable _listOfVariable;

        public GenerateQuery()
        {
            InitializeComponent();
            //_columns = new ListView.ListViewItemCollection(listView_GeneratetQuery);
        }
        public GenerateQuery(Library.Objects.ProcedureInfo proc)
        {
            InitializeComponent();
            //_columns = new ListView.ListViewItemCollection(listView_GeneratetQuery);
            _procedure = proc;
        }

        private void toolStripButton_SetColumns_Click(object sender, EventArgs e)
        {
            if (_procedure != null && _procedure.KnownVersionList.Count > 0 && _procedure.KnownVersionList[0].Parent.ListOfVariable != null)
            {
                _generateProperties = new Windows.frmQueryGeneratorProperties(_procedure.KnownVersionList[0].Parent.ListOfVariable);
                //foreach (Library.Objects.Variable tmpvar in _procedure.ProcSzczegNEW.Variables)
                //{
                //    //ListViewItem tmplistitem;
                //    listView_GeneratetQuery.Columns.Add(tmpvar.Name);
                //    //ListViewItem.ListViewSubItem tmplistsubitem = new ListViewItem.ListViewSubItem();

                //}
            }
            else
            {
                _generateProperties = _listOfVariable == null ? new Windows.frmQueryGeneratorProperties() : new Windows.frmQueryGeneratorProperties(_listOfVariable);
            }
            _generateProperties.ShowDialog(this);
            foreach (Library.Objects.Variable var in _generateProperties.ListOfVariable)
            {
                if (!listView_GeneratetQuery.Columns.ContainsKey(var.Name))
                    listView_GeneratetQuery.Columns.Add(var.Name, var.Name);
            }
            foreach (ColumnHeader colhed in listView_GeneratetQuery.Columns)
            {
                if (!_generateProperties.ListOfVariable.Contains(colhed.Text))
                {
                    listView_GeneratetQuery.Columns.RemoveByKey(colhed.Text);
                }
            }
            _listOfVariable = _generateProperties.ListOfVariable;
            _generateProperties.Dispose();

        }
    }
}
